/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hycreate.platform.modules.crm.mission;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hycreate.platform.common.config.Global;
import com.hycreate.platform.common.utils.StringUtils;
import com.hycreate.platform.common.web.BaseController;
import com.hycreate.platform.modules.crm.project.ProjectService;
import com.hycreate.platform.modules.sys.utils.UserUtils;

/**
 * 任务Controller
 * @author yl
 * @version 2016-12-07
 */
@Controller
@RequestMapping(value = "${adminPath}/crm/mission/mission")
public class MissionController extends BaseController {

	@Autowired
	private MissionService missionService;

	@Autowired
	private ProjectService projectService;

	@Autowired
	private WorkLoadService workLoadService;

	@ModelAttribute
	public Mission get(@RequestParam(required=false) String id) {
		Mission entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = missionService.get(id);
		}
		if (entity == null){
			entity = new Mission();
		}
		return entity;
	}

	//@RequiresPermissions("mission:mission:view")
	@RequestMapping(value = "list")
	public String list(  Model model) {
		String userId = UserUtils.getUser().getId();

		List<Mission> missionList =  missionService.getMission(userId);
		//System.out.println(missionList+"------------------------------------");
		model.addAttribute("mission", missionList);
		return "modules/crm/mission/missionList";
	}

	@RequiresPermissions("mission:mission:view")
	@RequestMapping(value = "form")
	public String form(Mission mission, Model model) {
		model.addAttribute("mission", mission);
		return "modules/crm/mission/missionForm";
	}

	//添加改变任务状态方法
	@RequestMapping(value = "status")
	public String status(Mission mission, HttpServletRequest request, HttpServletResponse response, Model model) {
		//System.out.println(mission.getId()+"--------------------------------------");
		mission.setStatus("已完成");
		missionService.save(mission);
		return "modules/crm/workbench/todayCalendarList";
	}

	@RequestMapping(value = "save")
	public String save(Mission mission, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, mission)){
			return form(mission, model);
		}
		missionService.save(mission);
		addMessage(redirectAttributes, "保存任务成功");
		return "redirect:"+Global.getAdminPath()+"/crm/mission/mission/list";
	}

	@RequiresPermissions("mission:mission:edit")
	@RequestMapping(value = "delete")
	public String delete(Mission mission, RedirectAttributes redirectAttributes) {
		missionService.delete(mission);
		addMessage(redirectAttributes, "删除任务成功");
		return "redirect:"+Global.getAdminPath()+"/crm/mission/mission/?repage";
	}

	/**
	 * 分配工作量
	 * @param workload
	 * @param model
	 * @return
	 */
	@RequiresPermissions("mission:mission:view")
	@RequestMapping(value = "workAllocation")
	public String workAllocation(String user,String num,Model model){
		String  userId=UserUtils.getUser().getId();  //获得当前用户的ID
		WorkLoad wl =new WorkLoad();
		wl.setDivider(user);
		wl.setAssigner(userId);
		wl.setTotalWork(Integer.parseInt(num));
		wl.setAccomplish(0);

		workLoadService.save(wl);

		return "redirect:"+Global.getAdminPath()+"/crm/mission/mission/workloadlist";
	}

	/**
	 * 获得部门的工作量情况
	 * @author zzf
	 * 2016年12月24日 16:27:29
	 * 
	 */
	@RequiresPermissions("mission:mission:view")
	@RequestMapping(value = "workloadlist")
	public String workloadlist(Model model){
		String  userId=UserUtils.getUser().getId();  //获得当前用户的ID
		List<WorkLoad> workloadList =	workLoadService.findworkLoadList(userId);
		List<String> repetitionList=new ArrayList<String>(); //重复的用户

		List<Staff> staffList=new LinkedList();//建立一个linkedlist方便删除重复的用户



		for (int i = 0; i < workloadList.size(); i++) {
			repetitionList.add(workloadList.get(i).getDivider());    //把已经查到的数据中用户提取出来
		}

		staffList =	workLoadService.findstaff(userId);
		if(repetitionList.size()>0){

			for (int i = 0; i < staffList.size(); i++) {

				for (int j = 0; j < repetitionList.size(); j++) {

					if(repetitionList.get(j).equals(staffList.get(i).getStaffId())){  //跟从其他表里得来的数据做对比
						staffList.remove(staffList.get(i));  //如果相同就删除这个对象
						i--;
						break;
					}

				}
			}
		}



		model.addAttribute("workloadList", workloadList);

		model.addAttribute("staffList", staffList);

		return "modules/crm/mission/WorkAllocation";
	}


	@RequiresPermissions("mission:mission:view")
	@RequestMapping(value = "superadditionWorkLoad")
	public String superadditionWorkLoad(@RequestParam("addnumb")String addnumb,@RequestParam("hiddnInputid")String hiddnInputid,Model model){

		WorkLoad workload=	workLoadService.get(hiddnInputid);

		workload.setTotalWork(workload.getTotalWork()+(Integer.parseInt(addnumb)));
		workLoadService.save(workload);

		return "redirect:"+Global.getAdminPath()+"/crm/mission/mission/workloadlist";
	}

	@RequiresPermissions("mission:mission:view")
	@RequestMapping(value = "cutWorkLoad")
	public String cutWorkLoad(@RequestParam("cutnumb")String cutnumb,@RequestParam("hiddnInputid2")String hiddnInputid2,Model model){

		WorkLoad workload=	workLoadService.get(hiddnInputid2);

		int yemianzhi=Integer.parseInt(cutnumb);
		int shujukuzhi=workload.getTotalWork();
		if(yemianzhi>shujukuzhi){


			String  userId=UserUtils.getUser().getId();  //获得当前用户的ID
			List<WorkLoad> workloadList =	workLoadService.findworkLoadList(userId);
			List<String> repetitionList=new ArrayList<String>(); //重复的用户

			List<Staff> staffList=new LinkedList();//建立一个linkedlist方便删除重复的用户



			for (int i = 0; i < workloadList.size(); i++) {
				repetitionList.add(workloadList.get(i).getDivider());    //把已经查到的数据中用户提取出来
			}

			staffList =	workLoadService.findstaff(userId);
			if(repetitionList.size()>0){

				for (int i = 0; i < staffList.size(); i++) {

					for (int j = 0; j < repetitionList.size(); j++) {

						if(repetitionList.get(j).equals(staffList.get(i).getStaffId())){  //跟从其他表里得来的数据做对比
							staffList.remove(staffList.get(i));  //如果相同就删除这个对象
							i--;
							break;
						}

					}
				}
			}

			String message="删减的值不正确";

			model.addAttribute("workloadList", workloadList);

			model.addAttribute("staffList", staffList);
			model.addAttribute("message", message);

			return "modules/crm/mission/WorkAllocation";

		}else{

			workload.setTotalWork(workload.getTotalWork()-(Integer.parseInt(cutnumb)));
			workLoadService.save(workload);

			return "redirect:"+Global.getAdminPath()+"/crm/mission/mission/workloadlist";

		}

	}


	
	
	@RequiresPermissions("mission:mission:view")
	@RequestMapping(value = "WorkLoadSelect")
	public String WorkLoadSelect(@RequestParam("dividerId")String dividerId,Model model){
		
		List<WorkLoad> postWorkloadList =	workLoadService.pastList(dividerId);
		
		System.out.println("----postWorkloadList------"+postWorkloadList);
		
		model.addAttribute("postWorkloadList", postWorkloadList);
		
		return "modules/crm/mission/WorkLoadSelect";
	}
	
	
	/**
	 * 个人工作量页面
	 * @param model
	 * @return
	 */
	@RequiresPermissions("mission:mission:view")
	@RequestMapping(value = "workload")
	public String workload(Model model){
		String userId = UserUtils.getUser().getId();
		WorkLoad workload = workLoadService.getWorkLoad(userId);
		
		model.addAttribute("workload", workload);
		return "modules/crm/mission/WorkLoad";
	}
	
	
	

}